#!/usr/bin/env python
# -*- coding: utf-8 -*-

import odoorpc
import threading
from settings import ODOO_HOST, ODOO_PORT, ODOO_DB, ODOO_USERNAME, ODOO_PASSWORD

Lock = threading.Lock()


class OdooManager(object):
    """
    功能：单件模式客户端。
    """

    __instance = None
    __odoo = None

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if (not cls.__instance or not cls.__odoo):
            try:
                Lock.acquire()
                # double check
                if not cls.__instance:
                    cls.__instance = super(OdooManager, cls).__new__(cls, *args, **kwargs)
                    cls.__odoo = odoorpc.ODOO(ODOO_HOST, port=ODOO_PORT)
                    cls.__odoo.login(ODOO_DB, ODOO_USERNAME, ODOO_PASSWORD)
            finally:
                Lock.release()
        return cls.__instance

    def get_instance(self):
        return self.__odoo

class ReportOdooManager(object):
    """
    报告单odoo链接
    功能：单件模式客户端。
    """

    __instance = None
    __odoo = None

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if (not cls.__instance or not cls.__odoo):
            try:
                Lock.acquire()
                # double check
                if not cls.__instance:
                    cls.__instance = super(ReportOdooManager, cls).__new__(cls, *args, **kwargs)
                    cls.__odoo = odoorpc.ODOO('140.246.134.213', port=8069)
                    cls.__odoo.login('develop', 'admin', 'Admin@XJ2048#$')
            finally:
                Lock.release()
        return cls.__instance

    def get_instance(self):
        return self.__odoo
